package com.zzxx.controller;

import com.zzxx.entity.*;
import com.zzxx.mapper.*;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class TimerTesk {
	@Autowired
	private final RuleMapper ruleMapper;
	@Autowired
	private TicketMapper ticketMapper;
	@Autowired
	private ActivityMapper activityMapper;
	@Autowired
	private TicketScheduleMapper ticketScheduleMapper;
	@Autowired
	private ActivityScheduleMapper activityScheduleMapper;

	public TimerTesk(RuleMapper ruleMapper, TicketMapper ticketMapper, ActivityMapper activityMapper, TicketScheduleMapper ticketScheduleMapper, ActivityScheduleMapper activityScheduleMapper) {
		this.ruleMapper = ruleMapper;
		this.ticketMapper = ticketMapper;
		this.activityMapper = activityMapper;
		this.ticketScheduleMapper = ticketScheduleMapper;
		this.activityScheduleMapper = activityScheduleMapper;

		Calendar creatTime = Calendar.getInstance();
		creatTime.set(Calendar.HOUR_OF_DAY,0);
		creatTime.set(Calendar.MINUTE,0);
		creatTime.set(Calendar.SECOND,1);
		creatTime.add(Calendar.DAY_OF_YEAR,1);
		Date time = creatTime.getTime();
		long delay = time.getTime() - new Date().getTime();
		Timer timer = new Timer();
		createdSchedule();
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				createdSchedule();
			}
		},delay,24*60*60*1000);
	}

	private void createdSchedule(){
		List<Rule> rules = ruleMapper.selectByExample(new RuleExample());
		for (int ri = 0; ri < rules.size(); ri++) {
			Rule rule = rules.get(ri);
			{
				Integer targettype = rule.getTargettype();
				String targetid = rule.getTargetid();
				Calendar startTime = Calendar.getInstance();
				startTime.setTime(rule.getStarttime());
				Calendar endTime = Calendar.getInstance();
				endTime.setTime(rule.getEndtime());
				//先查看是否已经生成过票
				if (targettype==0){
					TicketScheduleExample ticketScheduleExample = new TicketScheduleExample();
					ticketScheduleExample.createCriteria().andTicketidEqualTo(targetid);
					long count = ticketScheduleMapper.countByExample(ticketScheduleExample);
					if (count==0){
						for (int i = 0; i < rule.getBeforeday(); i++) {
							TicketSchedule tc = new TicketSchedule();
							Calendar tcStartTime = Calendar.getInstance();
							Calendar tcEndTime = Calendar.getInstance();

							tcStartTime.add(Calendar.DATE,i);
							tcStartTime.set(Calendar.HOUR_OF_DAY,startTime.get(Calendar.HOUR_OF_DAY));
							tcStartTime.set(Calendar.MINUTE,startTime.get(Calendar.MINUTE));
							tcStartTime.set(Calendar.SECOND,startTime.get(Calendar.SECOND));

							if (!rule.getWeekendcanbook()){
								if (tcStartTime.get(Calendar.DAY_OF_WEEK)== Calendar.SATURDAY ||tcStartTime.get(Calendar.DAY_OF_WEEK)== Calendar.SUNDAY) continue;
							}
							tcEndTime.add(Calendar.DATE,i);
							tcEndTime.set(Calendar.HOUR_OF_DAY,endTime.get(Calendar.HOUR_OF_DAY));
							tcEndTime.set(Calendar.MINUTE,endTime.get(Calendar.MINUTE));
							tcEndTime.set(Calendar.SECOND,endTime.get(Calendar.SECOND));

							tc.setId(UUID.randomUUID().toString().replaceAll("-",""));
							tc.setDate(tcStartTime.getTime());
							tc.setEndtime(tcEndTime.getTime());
							tc.setStarttime(tcStartTime.getTime());
							tc.setLastnum(rule.getTicketnum());
							tc.setTicketid(targetid);
							ticketScheduleMapper.insert(tc);
						}
					}else{
						TicketSchedule tc = new TicketSchedule();
						Calendar tcStartTime = Calendar.getInstance();
						Calendar tcEndTime = Calendar.getInstance();

						tcStartTime.add(Calendar.DATE,rule.getBeforeday()-1);
						tcStartTime.set(Calendar.HOUR_OF_DAY,startTime.get(Calendar.HOUR_OF_DAY));
						tcStartTime.set(Calendar.MINUTE,startTime.get(Calendar.MINUTE));
						tcStartTime.set(Calendar.SECOND,startTime.get(Calendar.SECOND));

						if (!rule.getWeekendcanbook()){
							if (tcStartTime.get(Calendar.DAY_OF_WEEK)== Calendar.SATURDAY ||tcStartTime.get(Calendar.DAY_OF_WEEK)== Calendar.SUNDAY) continue;
						}
						tcEndTime.add(Calendar.DATE,rule.getBeforeday()-1);
						tcEndTime.set(Calendar.HOUR_OF_DAY,endTime.get(Calendar.HOUR_OF_DAY));
						tcEndTime.set(Calendar.MINUTE,endTime.get(Calendar.MINUTE));
						tcEndTime.set(Calendar.SECOND,endTime.get(Calendar.SECOND));

						tc.setId(UUID.randomUUID().toString().replaceAll("-",""));
						tc.setDate(tcStartTime.getTime());
						tc.setEndtime(tcEndTime.getTime());
						tc.setStarttime(tcStartTime.getTime());
						tc.setLastnum(rule.getTicketnum());
						tc.setTicketid(targetid);

						TicketScheduleExample tsExample = new TicketScheduleExample();
						tsExample.setOrderByClause("date DESC");
						tsExample.createCriteria().andTicketidEqualTo(targetid);
						List<TicketSchedule> list = ticketScheduleMapper.selectByExample(tsExample);
						Calendar lastDay = Calendar.getInstance();
						lastDay.setTime(list.get(0).getDate());
						Calendar tcDay = Calendar.getInstance();
						tcDay.setTime(tc.getDate());
						if (
						!(lastDay.get(Calendar.YEAR)==tcDay.get(Calendar.YEAR))||
						!(lastDay.get(Calendar.MONTH)==tcDay.get(Calendar.MONTH))||
						!(lastDay.get(Calendar.DAY_OF_YEAR)==tcDay.get(Calendar.DAY_OF_YEAR))
						){
							ticketScheduleMapper.insert(tc);
						}
					}
				}else{
					ActivityScheduleExample activityScheduleExample = new ActivityScheduleExample();
					activityScheduleExample.createCriteria().andActivityidEqualTo(targetid);
					long count = activityScheduleMapper.countByExample(activityScheduleExample);
					if (count==0){
						for (int i = 0; i < rule.getBeforeday(); i++) {
							ActivitySchedule ac = new ActivitySchedule();
							Calendar acStartTime = Calendar.getInstance();
							Calendar acEndTime = Calendar.getInstance();

							acStartTime.add(Calendar.DATE,i);
							acStartTime.set(Calendar.HOUR_OF_DAY,startTime.get(Calendar.HOUR_OF_DAY));
							acStartTime.set(Calendar.MINUTE,startTime.get(Calendar.MINUTE));
							acStartTime.set(Calendar.SECOND,startTime.get(Calendar.SECOND));

							if (!rule.getWeekendcanbook()){
								if (acStartTime.get(Calendar.DAY_OF_WEEK)== Calendar.SATURDAY ||
								acStartTime.get(Calendar.DAY_OF_WEEK)== Calendar.SUNDAY) continue;
							}
							acEndTime.add(Calendar.DATE,i);
							acEndTime.set(Calendar.HOUR_OF_DAY,endTime.get(Calendar.HOUR_OF_DAY));
							acEndTime.set(Calendar.MINUTE,endTime.get(Calendar.MINUTE));
							acEndTime.set(Calendar.SECOND,endTime.get(Calendar.SECOND));

							ac.setId(UUID.randomUUID().toString().replaceAll("-",""));
							ac.setAdate(acStartTime.getTime());
							ac.setEndtime(acEndTime.getTime());
							ac.setStarttime(acStartTime.getTime());
							ac.setFreenum(rule.getTicketnum());
							ac.setActivityid(targetid);
							activityScheduleMapper.insert(ac);
						}
					}else{
						ActivitySchedule ac = new ActivitySchedule();
						Calendar acStartTime = Calendar.getInstance();
						Calendar acEndTime = Calendar.getInstance();

						acStartTime.add(Calendar.DATE,rule.getBeforeday()-1);
						acStartTime.set(Calendar.HOUR_OF_DAY,startTime.get(Calendar.HOUR_OF_DAY));
						acStartTime.set(Calendar.MINUTE,startTime.get(Calendar.MINUTE));
						acStartTime.set(Calendar.SECOND,startTime.get(Calendar.SECOND));

						if (!rule.getWeekendcanbook()){
							if (acStartTime.get(Calendar.DAY_OF_WEEK)== Calendar.SATURDAY ||acStartTime.get(Calendar.DAY_OF_WEEK)== Calendar.SUNDAY) continue;
						}
						acEndTime.set(Calendar.DATE,rule.getBeforeday()-1);
						acEndTime.set(Calendar.HOUR_OF_DAY,endTime.get(Calendar.HOUR_OF_DAY));
						acEndTime.set(Calendar.MINUTE,endTime.get(Calendar.MINUTE));
						acEndTime.set(Calendar.SECOND,endTime.get(Calendar.SECOND));

						ac.setId(UUID.randomUUID().toString().replaceAll("-",""));
						ac.setAdate(acStartTime.getTime());
						ac.setEndtime(acEndTime.getTime());
						ac.setStarttime(acStartTime.getTime());
						ac.setFreenum(rule.getTicketnum());
						ac.setActivityid(targetid);
						ActivityScheduleExample asExample = new ActivityScheduleExample();
						asExample.setOrderByClause("adate DESC");
						asExample.createCriteria().andActivityidEqualTo(targetid);

						List<ActivitySchedule> list = activityScheduleMapper.selectByExample(asExample);
						Date adate = list.get(0).getAdate();
						Calendar lastDay = Calendar.getInstance();
						lastDay.setTime(adate);
						Calendar acDay = Calendar.getInstance();
						acDay.setTime(ac.getAdate());
						if (!(lastDay.get(Calendar.YEAR) ==acDay.get(Calendar.YEAR))||
						!(lastDay.get(Calendar.MONTH) ==acDay.get(Calendar.MONTH))||
						!(lastDay.get(Calendar.DAY_OF_YEAR) ==acDay.get(Calendar.DAY_OF_YEAR))){
							activityScheduleMapper.insert(ac);
						}
					}
				}
			}
		}
		//删除过期票
		TicketScheduleExample tse = new TicketScheduleExample();
		tse.createCriteria().andStarttimeLessThan(new Date());
		ticketScheduleMapper.deleteByExample(tse);
		ActivityScheduleExample ase = new ActivityScheduleExample();
		ase.createCriteria().andStarttimeLessThan(new Date());
		activityScheduleMapper.deleteByExample(ase);
	}
}
